<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>EnvironmentTest</title>

<link rel="stylesheet"
	href="http://code.jquery.com/qunit/qunit-1.15.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.15.0.js"></script>

<script src="../../main/js/util/JavaScriptAugments.js"></script>
<script src="../../main/js/util/Logger.js"></script>
<script src="../../main/js/util/InputValidator.js"></script>
<script src="../../main/js/util/ArrayUtilities.js"></script>

<script src="../../main/js/Path.js"></script>
<script src="../../main/js/Phase.js"></script>
<script src="../../main/js/Team.js"></script>
<script src="../../main/js/Maneuver.js"></script>
<script src="../../main/js/ShipAction.js"></script>
<script src="../../main/js/Ship.js"></script>
<script src="../../main/js/RangeRuler.js"></script>
<script src="../../main/js/Weapon.js"></script>
<script src="../../main/js/Pilot.js"></script>
<script src="../../main/js/Position.js"></script>
<script src="../../main/js/DamageCard.js"></script>
<script src="../../main/js/UpgradeCard.js"></script>
<script src="../../main/js/Token.js"></script>
<script src="../../main/js/SquadBuilder.js"></script>
<script src="../../main/js/Environment.js"></script>
<script src="../../main/js/PlanningAction.js"></script>
<script src="../../main/js/SimpleAgent.js"></script>

<script src="../../main/js/ui/ImageUtilities.js"></script>
<script src="../../main/js/ui/HumanAgent.js"></script>

<script>
    var LOGGER = new Logger();

    QUnit.test("Environment.getPositionFor()", function(assert)
    {
        Token.resetNextId();
        var position = new Position(1, 2, 3);
        var environment = Environment.createCoreSetEnvironment();
        var agent = environment.getImperialAgent();
        var token = new Token(Pilot.ACADEMY_PILOT, agent);
        environment.placeToken(position, token);

        assert.strictEqual(environment.getPositionFor(token), position);
    });

    QUnit.test("Environment.getTargetableDefenders() none", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();
        var attackerPosition = new Position(458, 895, -90);
        var attacker = environment.getTokenAt(attackerPosition);
        var weapon = attacker.getPrimaryWeapon();

        // Run.
        var result = environment.getTargetableDefenders(attacker,
                attackerPosition, weapon);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 0);
    });

    QUnit.test("Environment.getTargetableDefenders() one", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();
        var attackerPosition = new Position(458, 895, -90);
        var attacker = environment.getTokenAt(attackerPosition);
        environment.removeToken(attackerPosition);
        var attackerPosition = new Position(305, 70, -90);
        environment.placeToken(attackerPosition, attacker);
        var weapon = attacker.getPrimaryWeapon();

        // Run.
        var result = environment.getTargetableDefenders(attacker,
                attackerPosition, weapon);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 1);
    });

    QUnit.test("Environment.getTargetableDefendersAtRange() none", function(
            assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();
        var attackerPosition = new Position(458, 895, -90);
        var attacker = environment.getTokenAt(attackerPosition);
        var weapon = attacker.getPrimaryWeapon();

        // Run.
        var result = environment.getTargetableDefendersAtRange(attacker,
                attackerPosition, weapon, Range.ONE);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 0);
    });

    QUnit.test("Environment.getTargetableDefendersAtRange() one", function(
            assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();
        var attackerPosition = new Position(458, 895, -90);
        var attacker = environment.getTokenAt(attackerPosition);
        environment.removeToken(attackerPosition);
        var attackerPosition = new Position(305, 70, -90);
        environment.placeToken(attackerPosition, attacker);
        var weapon = attacker.getPrimaryWeapon();

        // Run.
        var result = environment.getTargetableDefendersAtRange(attacker,
                attackerPosition, weapon, Range.ONE);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 1);
    });

    QUnit.test("Environment.getTokenAt()", function(assert)
    {
        Token.resetNextId();
        var position = new Position(1, 2, 3);
        var environment = Environment.createCoreSetEnvironment();
        var agent = environment.getImperialAgent();
        var token = new Token(Pilot.ACADEMY_PILOT, agent);
        environment.placeToken(position, token);

        assert.strictEqual(environment.getTokenAt(position), token);
    });

    QUnit.test("Environment.getTokenAt() 1", function(assert)
    {
        Token.resetNextId();
        var position = new Position(305, 20, 90);
        var environment = Environment.createCoreSetEnvironment();

        var token = environment.getTokenAt(position);
        assert.strictEqual(token.getPilot(), Pilot.MAULER_MITHEL);
    });

    QUnit.test("Environment.getTokenAt() 2", function(assert)
    {
        Token.resetNextId();
        var position = new Position(610, 20, 90);
        var environment = Environment.createCoreSetEnvironment();

        var token = environment.getTokenAt(position);
        assert.strictEqual(token.getPilot(), Pilot.DARK_CURSE);
    });

    QUnit.test("Environment.getTokenAt() 3", function(assert)
    {
        Token.resetNextId();
        var position = new Position(458, 895, -90);
        var environment = Environment.createCoreSetEnvironment();

        var token = environment.getTokenAt(position);
        assert.strictEqual(token.getPilot(), Pilot.LUKE_SKYWALKER);
    });

    QUnit.test("Environment.getTokenCountFor()", function(assert)
    {
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        assert.strictEqual(environment.getTokenCountFor(Team.IMPERIAL), 2);
        assert.strictEqual(environment.getTokenCountFor(Team.REBEL), 1);
    });

    QUnit.test("Environment.getTokens()", function(assert)
    {
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        var tokens = environment.getTokens();
        assert.equal(tokens.length, 3);
        assert.equal(tokens[0].getPilot(), Pilot.MAULER_MITHEL);
        assert.equal(tokens[1].getPilot(), Pilot.DARK_CURSE);
        assert.equal(tokens[2].getPilot(), Pilot.LUKE_SKYWALKER);
    });

    QUnit.test("Environment.getTokensForActivation()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run.
        var result = environment.getTokensForActivation();

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 3);
        {
            var token = result[0];
            assert.equal(token.getId(), 2);
            assert.equal(token.getPilot(), Pilot.DARK_CURSE);
        }
        {
            var token = result[1];
            assert.equal(token.getId(), 1);
            assert.equal(token.getPilot(), Pilot.MAULER_MITHEL);
        }
        {
            var token = result[2];
            assert.equal(token.getId(), 3);
            assert.equal(token.getPilot(), Pilot.LUKE_SKYWALKER);
        }
    });

    QUnit.test("Environment.getTokensForCombat()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run.
        var result = environment.getTokensForCombat();

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 3);
        {
            var token = result[0];
            assert.equal(token.getId(), 3);
            assert.equal(token.getPilot(), Pilot.LUKE_SKYWALKER);
        }
        {
            var token = result[1];
            assert.equal(token.getId(), 1);
            assert.equal(token.getPilot(), Pilot.MAULER_MITHEL);
        }
        {
            var token = result[2];
            assert.equal(token.getId(), 2);
            assert.equal(token.getPilot(), Pilot.DARK_CURSE);
        }
    });

    QUnit.test("Environment.getTokensForTeam() Imperial", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run.
        var result = environment.getTokensForTeam(Team.IMPERIAL);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 2);
        assert.equal(result[0].getPilot(), Pilot.MAULER_MITHEL);
        assert.equal(result[1].getPilot(), Pilot.DARK_CURSE);
    });

    QUnit.test("Environment.getTokensForTeam() Rebel", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run.
        var result = environment.getTokensForTeam(Team.REBEL);

        // Verify.
        assert.ok(result);
        assert.equal(result.length, 1);
        assert.equal(result[0].getPilot(), Pilot.LUKE_SKYWALKER);
    });

    QUnit.test("Environment.incrementRound()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();
        assert.equal(environment.getRound(), 0);

        // Run.
        environment.incrementRound();

        // Verify.
        assert.equal(environment.getRound(), 1);
    });

    QUnit.test("Environment.placeToken()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var position = new Position(1, 2, 3);
        var environment = Environment.createCoreSetEnvironment();
        var agent = environment.getImperialAgent();
        var token = new Token(Pilot.ACADEMY_PILOT, agent);

        // Run.
        environment.placeToken(position, token);

        // Verify.
        assert.strictEqual(environment.getPositionFor(token), position);
        assert.strictEqual(environment.getTokenAt(position), token);
    });

    QUnit.test("Environment.removeToken()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var position = new Position(1, 2, 3);
        var environment = Environment.createCoreSetEnvironment();
        var agent = environment.getImperialAgent();
        var token = new Token(Pilot.ACADEMY_PILOT, agent);
        environment.placeToken(position, token);
        assert.strictEqual(environment.getPositionFor(token), position);
        assert.strictEqual(environment.getTokenAt(position), token);

        // Run.
        environment.removeToken(position);

        // Verify.
        assert.strictEqual(environment.getPositionFor(token), undefined);
        assert.strictEqual(environment.getTokenAt(position), undefined);
    });

    QUnit.test("Environment.setPhase()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run.
        environment.setPhase(Phase.ACTIVATION_REVEAL_DIAL);

        // Verify.
        assert.equal(environment.getPhase(), Phase.ACTIVATION_REVEAL_DIAL);
    });

    QUnit.test("Environment.toString()", function(assert)
    {
        // Setup.
        Token.resetNextId();
        var environment = Environment.createCoreSetEnvironment();

        // Run / Verify.
        assert.equal(environment.toString(),
                "(305, 20, 90) 1 \"Mauler Mithel\" (TIE Fighter)\n"
                        + "(610, 20, 90) 2 \"Dark Curse\" (TIE Fighter)\n"
                        + "(458, 895, 270) 3 Luke Skywalker (X-Wing)\n");
    });
</script>
</head>
<body>
	<div id="qunit"></div>
</body>
</html>
